package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class invokeCMD {
	public void runCommand(String command, boolean outFlag){
		try {

            Process p = Runtime.getRuntime().exec(new String[]{"bash","-c",command});
            if (outFlag){
            	// Print result 
                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line=null;
                
                while((line=input.readLine()) != null) {
                    System.out.println(line);
                }
            }
            int exitVal = p.waitFor();
            System.out.println("Exited with error code "+exitVal);
            if (exitVal !=0 ){
            	// Some thing wrong here => output to check 
                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line=null;

                while((line=input.readLine()) != null) {
                    System.out.println(line);
                }            	
            }            
        } catch(Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
		 
	}

}
